package com.itheima.health.controller;


import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.io.IOException;
import java.io.InputStream;
import java.util.*;

@RestController
@RequestMapping("/ordersetting")
@Slf4j
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;
    /*
    预约设置的文件的上传
     */
    @PostMapping("/upload")  //解析excel 文件 二进制
    public Result upload(@RequestParam("excelFile")  MultipartFile file) {
        //拿一下文件的名字
        String filename = file.getOriginalFilename();
        //判断一下有没有文件名
        if (StringUtils.isEmpty(filename)) {
            return new Result(false, "文件名为空");
        }
        Workbook workbook = null; //创建空的工作簿
        try (InputStream inputStream = file.getInputStream();) {//把文件从流中读出来
            //判断字符串后缀名是否相等
            if (filename.endsWith(".xlsx")) {  //如果这个后缀名是.xlsx的就把数据读到工作簿中
                workbook = new XSSFWorkbook(inputStream);//读取xlsx格式的文件
            } else if (filename.endsWith(".xls")) {
                workbook = new HSSFWorkbook(inputStream);  //读取xls文件
            } else {
                return new Result(false, "文件格式不对,请您重新上传");
            }
        } catch (IOException e) {
            return new Result(false, "上传失败");
        }
        //创建一个OrderString集合准备接收文件
        List<OrderSetting> orderSettings = new ArrayList<>();
        log.info("orderSettings数据是{}",orderSettings);
        //遍历把数据拿出来
        //log.info("orderSetting数据是:{}",orderSetting);
        for (Sheet sheet : workbook) {
            //遍历工作簿拿到每一sheet页
            //不要第一行表头从第二行拿去
            for (int i = 1; i < sheet.getLastRowNum(); i++) {  //遍历sheet页上面的每一行数据
                //拿到每一行数据
                Row row = sheet.getRow(i);
                //log.info("row这个值是什么:{}",row);
                Cell datecell = row.getCell(0); //拿日期列
                //orderSetting.setOrderDate(datecell.getDateCellValue());
               // log.info("是日期吗?{}",datecell);
                Cell numcell = row.getCell(1); //拿预约列
                OrderSetting orderSetting = new OrderSetting();
               // orderSetting.setNumber((int)numcell.getNumericCellValue());
                if (datecell != null) { //判断每一个日期单元格是不是为空
                        Date dateCellValue = datecell.getDateCellValue();
                        log.info("每个日期单元格是:{}", dateCellValue);
                    orderSetting.setOrderDate(datecell.getDateCellValue());
                }else{
                    break;
                }
                if (numcell != null) { //判断每一个预约单元格是不是为空
                        int numericCellValue = (int) numcell.getNumericCellValue();
                       log.info("每个预约单元格是:{}", numericCellValue);
                    orderSetting.setNumber((int) numcell.getNumericCellValue());
                }else{
                    break;
                }
                orderSettings.add(orderSetting);
                log.info("这个值是{}",orderSetting);

            }


        }

        orderSettingService.add(orderSettings);
        return new Result(true,MessageConst.IMPORT_ORDERSETTING_SUCCESS);
    }




    @GetMapping("/getOrderSettingByMonth")
    public Result getOrderSettingByMonth(@RequestParam Integer year ,@RequestParam Integer month){
        //service进行调用
        List<OrderSetting> orderSettingByMonth = orderSettingService.getOrderSettingByMonth(year, month);
        /*
        转换为前端要的数据结构
        year: 2022
        month: 8
         */
        List<Map<String,Object>> maps = new ArrayList<>();
        //遍历月份集合
        for (OrderSetting orderSetting : orderSettingByMonth) {
            //创建一个日历类  调用内部提供的静态方法进行创建  Calendar底层提供的getInstance方法
            Calendar calendar = Calendar.getInstance();
            //把数据库里面的时间设置为预约时间
            calendar.setTime(orderSetting.getOrderDate());
            Map<String, Object> map = new HashMap<>();
            //主要作用是calendar.get(DAY_OF_MONTH)，用来获得这一天在是这个月的第多少天
            map.put("date",calendar.get(Calendar.DAY_OF_MONTH));
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            maps.add(map);
            log.info("这个集合是{}",maps);

        }
        return new Result(true,MessageConst.GET_ORDERSETTING_SUCCESS,maps);
    }

    /*
    根据id进行修改
     */
    @RequestMapping("/editNumberByDate")
    public Result editNumberByDate(@RequestBody OrderSetting orderSetting) {
        log.info("根据id修改的是{}",orderSetting);
            //根据日期进行预约人数的修改
        orderSettingService.updateByOrderId(orderSetting.getNumber(), orderSetting.getOrderDate());
        return new Result(true,MessageConst.ORDERSETTING_SUCCESS);
    }

}
